/*
 *      ajax.js
 *
 *      Copyright 2010 Rakitin Vitaliy <virak@ukr.net>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */
var http = false;

if(navigator.appName == "Microsoft Internet Explorer") {
  http = new ActiveXObject("Microsoft.XMLHTTP");
  } else {
    http = new XMLHttpRequest();
    }

function message(element, event, param1, param2) {

    var url = "message.php";
    var params = "element="+element+"&event="+event+"&param1="+param1+"&param2="+param2;

    http.open("POST", url, true);

    //Send the proper header information along with the request
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    //http.setRequestHeader("Content-length", params.length);
    //http.setRequestHeader("Connection", "close");

    http.onreadystatechange = function() {//Call a function when the state changes.
	if(http.readyState == 4 )
        if(http.status == 200 )
        {
            var response;
			response = eval(http.responseText);
            for(var i=0; i<=response.length-1; i++) {
				switch (response[i][0]) {
					case 'inner':
                        document.getElementById(response[i][1]).innerHTML = response[i][2];
                        break;
					case 'update':
                        //var a=document.activeElement;
                        document.getElementById(response[i][1]).outerHTML = response[i][2];
                        //a.focus();
                        break;
                    case 'append':
						var div = document.createElement("div");
						document.getElementById(response[i][1]).appendChild(div);
                        div.outerHTML = response[i][2];
                        break;
                    case 'modify':
						//var elem = document.getElementById(response[i][1]);
						if (response[i][2] == "value") {
							document.getElementById(response[i][1]).value = response[i][3];
						} else {
							document.getElementById(response[i][1]).setAttribute(response[i][2],response[i][3]);
						}
						//elem.setAttribute("value","56");
						//document.getElementById(response[i][1]).setAttribute(response[i][2],response[i][3]);
						//document.getElementById(response[i][1]).setAttribute("style", "top:10px");
						//var div = document.createElement("div");
						//document.body.appendChild(div);
						//div.outerHTML = response[i][2];
						//document.body.replaceChild(document.getElementById(response[i][1]),div);
						////document.getElementById(response[i][1]).outerHTML = response[i][2];
                        break;
                    case 'delete':
						//document.body.removeChild(document.getElementById(response[i][1]));
						document.getElementById(response[i][1]).outerHTML = '';
                        break;
                }

            }
		}
    }
    http.send(params);
}
